﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Clarendon.AzureBilling.Library
{
    /// <summary>
    /// A delegate used to return the hosted services to the client
    /// </summary>
    public delegate void AzureHostedServiceEventHandler(AzureResponseEventArgs args);
    /// <summary>
    /// Base class containing the methods which should be overriden for a particular request type
    /// </summary>
    public abstract class RequestAzureService
    {
        /// <summary>
        /// An instance of the ServiceManager class
        /// </summary>
        protected internal ServiceManager Manager;
        /// <summary>
        /// An Xml parser to specifically parse the relevant and routed Xml from the response
        /// </summary>
        protected internal IParseResponseXmlServices Parser;
       
        /// <summary>
        /// Default construcor used to populate the ServiceManager class
        /// </summary>
        public RequestAzureService(ServiceManager manager)
        {
            Manager = manager;
        }

        /// <summary>
        /// Derived classes should override this method to ensure that they can process the async reponse from Azure
        /// </summary>
        public abstract void Populate(HttpWebResponse response);
        /// <summary>
        /// Gets the service details based on the implementation of the requester 
        /// </summary>
        public abstract void MakeServiceRequest();

        /// <summary>
        /// Used to make a blocking request to signal back to the application to take control from the main thread 
        /// </summary>
        protected void ReleaseSemaphore()
        {
            // release the mutex to signal to the client that the async call has returned successfully
            if (Manager.Semaphore != null)
                Manager.Semaphore.Release();
        }

        /// <summary>
        /// Returns a ServiceManagementRequest with the correct subscription id and base URI for the Azure request
        /// </summary>
        protected ServiceManagementRequest GetServiceManagementRequest(string operationId)
        {
            return new ServiceManagementRequest
            {
                BaseUri = Manager.ManagementBaseUri,
                SubscriptionId = Manager.SubscriptionId,
                OperationId = operationId,
                ServiceType = Manager.HostedServiceRequest
            };
        }
    }
}
